package com.go.where.manager.action.go;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.CustomerInfo;
import com.go.where.manager.pojo.MessageInfo;
import com.go.where.manager.service.CustomerService;
import com.go.where.manager.service.MessageService;

public class MessageAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource(name = "messageService")
	private MessageService messageService;
	@Resource(name = "customerService")
	private CustomerService customerService;

	private List<String> customerIds;
	private List<String> invitationCodes;
	private Pagination<MessageInfo> page;
	private MessageInfo messageInfo;
	private MessageInfo messageInfoQuery;

	private String sendCustomerType;

	private String messageId;
	private String customerId;
	private String invitationCode;

	private List<CustomerInfo> customerInfos;

	public String list() {
		try {
			if (messageInfoQuery == null) {
				messageInfoQuery = new MessageInfo();
				messageInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
				messageInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			messageInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = messageService.queryPage(messageInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	/**
	 * 假删除
	 * 
	 * @return
	 */
	public String falseDelete() {
		try {
			messageInfo = new MessageInfo();
			messageInfo.setMessageId(messageId);
			messageInfo.setIsDelete(Constants.DELETED);
			messageService.update(messageInfo);
			redirectionUrl = "message!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除消息信息异常", e);
			addActionError("删除消息信息异常");
			return LIST;
		}
	}

	public String input() {
		return INPUT;
	}

	public String save() {
		try {
			String sendCustomerType = messageInfo.getMessageType();
			if (StringUtils
					.equals(sendCustomerType, Constants.INVITATION_USERS)) {
				CustomerInfo cinfo = new CustomerInfo();
				cinfo.setStatus(Constants.LOCK);
				cinfo.setInvitationCode(invitationCode);
				customerIds = customerService.queryCustomerIds(cinfo);
			} else if (StringUtils
					.equals(sendCustomerType, Constants.ALL_USERS)) {
				CustomerInfo cinfo = new CustomerInfo();
				cinfo.setStatus(Constants.LOCK);
				customerIds = customerService.queryCustomerIds(cinfo);
			} else {
				customerIds = new ArrayList<String>();
				customerIds.add(customerId);
			}
			String msg = messageService.saveMessage(messageInfo, customerIds,
					sendCustomerType);
			if (StringUtils.isNotEmpty(msg)) {
				flag = false;
				addActionError(msg);
			}
			redirectionUrl = "message!list.htm";
			return "message_success";
		} catch (Exception e) {
			log.error("发送消息出错", e);
			addActionError("发送消息出错" + e.getMessage());
			return INPUT;
		}
	}

	public Pagination<MessageInfo> getPage() {
		return page;
	}

	public void setPage(Pagination<MessageInfo> page) {
		this.page = page;
	}

	public String getSendCustomerType() {
		return sendCustomerType;
	}

	public void setSendCustomerType(String sendCustomerType) {
		this.sendCustomerType = sendCustomerType;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public MessageInfo getMessageInfo() {
		return messageInfo;
	}

	public void setMessageInfo(MessageInfo messageInfo) {
		this.messageInfo = messageInfo;
	}

	public List<String> getCustomerIds() {
		return customerIds;
	}

	public void setCustomerIds(List<String> customerIds) {
		this.customerIds = customerIds;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public List<String> getInvitationCodes() {
		return invitationCodes;
	}

	public void setInvitationCodes(List<String> invitationCodes) {
		this.invitationCodes = invitationCodes;
	}

	public String getInvitationCode() {
		return invitationCode;
	}

	public void setInvitationCode(String invitationCode) {
		this.invitationCode = invitationCode;
	}

	public List<CustomerInfo> getCustomerInfos() {
		return customerInfos;
	}

	public void setCustomerInfos(List<CustomerInfo> customerInfos) {
		this.customerInfos = customerInfos;
	}

	public MessageInfo getMessageInfoQuery() {
		return messageInfoQuery;
	}

	public void setMessageInfoQuery(MessageInfo messageInfoQuery) {
		this.messageInfoQuery = messageInfoQuery;
	}

}
